package org.ai.demo.gateway;

import org.ai.demo.gateway.filter.PostRequestFilter;
import org.ai.demo.gateway.filter.PreRequestFilter;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
import org.springframework.cloud.netflix.zuul.filters.discovery.PatternServiceRouteMapper;
import org.springframework.context.annotation.Bean;

@SpringBootApplication
@EnableDiscoveryClient
@EnableZuulProxy
//@EnableOAuth2Sso
public class GatewayApplication {

	public static void main(String[] args) {
		SpringApplication.run(GatewayApplication.class, args);
	}
	
	/**
     * 资源过滤器
     * @return 资源过滤器
     */
    @Bean
    public PreRequestFilter accessFilter(){
        return new PreRequestFilter();
    }
    
    @Bean
    public PostRequestFilter postFilter(){
        return new PostRequestFilter();
    }
    
    /**
     * 生成版本号作为路由前缀的路由规则
     * 如服务名为userservice-v1、userservice-v2
     * 路由匹配/v1/userservice、/v2/userservice
     */
    @Bean
    public PatternServiceRouteMapper serviceRouteMapper(){
    	return new PatternServiceRouteMapper(
    			"(?<name>^.+)-(?<version>v.+$)",
    			"${version}/${name}");
    }
}
